﻿using System;
using System.ComponentModel;

namespace NLite
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class BooleanDisposable : IBooleanDisposable
    {
        [NonSerialized]
        private readonly object SynchRoot = new object();
        [NonSerialized]
        private bool _isDisposed;

        /// <summary>
        /// 
        /// </summary>
        ~BooleanDisposable()
        {
            Dispose(false);
        }

        #region IDisposable Members
        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            lock (SynchRoot)
            {
                if (!IsDisposed)
                {
                    _isDisposed = true;
                    Dispose(true);
                    GC.SuppressFinalize(this);
                }
            }
        }

        #endregion
        /// <summary>
        /// 
        /// </summary>
        [Browsable(false)]
        public bool IsDisposed { get { return _isDisposed; } }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        protected virtual void CheckNotDisposed()
        {
            if (_isDisposed)
            {
                var ex = new ObjectDisposedException(GetType().Name);
                throw ex.Handle();
            }
        }
    }
}
